const express = require('express')
const cors = require('cors')
const router = require('./router')
const errHandler = require('./middleware/error-handler')
const Handler404 = require('./middleware/404-handler')

const app = express()

// 静态资源目录
app.use(express.static(__dirname + '/public'));

// 跨域中间件
app.use(cors())
// 解析表单请求体 application/json
app.use(express.json())
// 解析表单请求体 application/x-www-form-urlencoded
app.use(express.urlencoded())

// 注册路由
app.use('/api', router)

// 404中间件
app.use(Handler404())

// 错误处理中间件
app.use(errHandler())

app.listen(3000, () => {
    console.log('server running at http://localhost:3000')
})